﻿#include "mypushbuttoncopy_p.h"

MyPushButtonCopyPrivate::MyPushButtonCopyPrivate()
    : imageLayerIndex(1),
      sourceDeviceIndex(1),
      destinationDeviceIndex(1),
      objectType(My::M),
      sourceObjectIndex(0),
      destinationObjectIndex(0),
      displayEnable(true),
      displayDefault(true),
      pushButtonCopyOperate(My::Coil_Copy),
      coilOfRegisterIndex(0),
      textBuffer(),
      LeftmousePressed(false),
      mousePressPoint(0, 0)
{

}

MyPushButtonCopyPrivate::~MyPushButtonCopyPrivate()
{
}

MyPushButtonCopy::MyPushButtonCopy(QWidget *parent)
    : QPushButton(parent),
      d(new MyPushButtonCopyPrivate)
{

}

MyPushButtonCopy::~MyPushButtonCopy()
{
    delete d;
}

/*!
    \property MyPushButtonCopy::imageLayerIndex
    \brief the index of the imageLayer

    The imageLayer is the layer of the widget.

    The default imageLayerIndex is 1.

    \sa indent
*/
int MyPushButtonCopy::getImageLayerIndex() const
{
    return d->imageLayerIndex;
}

void MyPushButtonCopy::setImageLayerIndex(int imageLayerInd)
{
    if(d->imageLayerIndex == imageLayerInd)
        return;
    d->imageLayerIndex = imageLayerInd;
}

/*!
    \property MyPushButtonCopy::sourceDeviceIndex
    \brief the index of the paired sourceDevice

    The default sourceDeviceIndex is 0.

    \sa indent
*/
int MyPushButtonCopy::getSourceDeviceIndex() const
{
    return d->sourceDeviceIndex;
}

void MyPushButtonCopy::setSourceDeviceIndex(int deviceInd)
{
    if(d->sourceDeviceIndex == deviceInd)
        return;
    d->sourceDeviceIndex = deviceInd;
}

/*!
    \property MyPushButtonCopy::destinationDeviceIndex
    \brief the index of the paired destinationDevice

    The default destinationDevice is 0.

    \sa indent
*/
int MyPushButtonCopy::getDestinationDeviceIndex() const
{
    return d->destinationDeviceIndex;
}

void MyPushButtonCopy::setDestinationDeviceIndex(int deviceInd)
{
    if(d->destinationDeviceIndex == deviceInd)
        return;
    d->destinationDeviceIndex = deviceInd;
}

/*!
    \property MyPushButtonCopy::objectType
    \brief the type of the object

    The object is coil or register of PLC.

    The default objectType is M.

    \sa indent
*/
My::ObjectType MyPushButtonCopy::getObjectType() const
{
    return d->objectType;
}

void MyPushButtonCopy::setObjectType(My::ObjectType type)
{
    if(d->objectType == type)
        return;
    d->objectType = type;
}

/*!
    \property MyPushButtonCopy::sourceObjectIndex
    \brief the sourceObjectIndex of the data copied

    The default sourceObjectIndex is 0.

    \sa indent
*/
int MyPushButtonCopy::getSourceObjectIndex() const
{
    return d->sourceObjectIndex;
}

void MyPushButtonCopy::setSourceObjectIndex(int sourceObjIndex)
{
    if(d->sourceObjectIndex == sourceObjIndex)
        return;
    d->sourceObjectIndex = sourceObjIndex;
}

/*!
    \property MyPushButtonCopy::destinationObjectIndex
    \brief the destinationObjectIndex of the data copied

    The default destinationObjectIndex is 0.

    \sa indent
*/
int MyPushButtonCopy::getDestinationObjectIndex() const
{
    return d->destinationObjectIndex;
}

void MyPushButtonCopy::setDestinationObjectIndex(int destiObjIndex)
{
    if(d->destinationObjectIndex == destiObjIndex)
        return;
    d->destinationObjectIndex = destiObjIndex;
}

/*!
    \property MyPushButtonCopy::displayEnable
    \brief control the MyLabel whether or not display

    True or False corresponds to display or undisplay.

    The default DisplayEnable is true.

    \sa indent
*/
bool MyPushButtonCopy::getDisplayEnable() const
{
    return d->displayEnable;
}

void MyPushButtonCopy::setDisplayEnable(bool displayEnabl)
{
    if(d->displayEnable == displayEnabl)
        return;
    d->displayEnable = displayEnabl;

    if(!d->displayEnable)
    {
        d->textBuffer = text();
        setText(NULL);
    }
    else
    {
        setText(d->textBuffer);
    }

    this->update();
}

/*!
    \property MyPushButtonCopy::displayDefault
    \brief control the content to display

    True or False corresponds to default or notdefault.

    The default displayDefault is true.

    \sa indent
*/
bool MyPushButtonCopy::getDisplayDefault() const
{
    return d->displayDefault;
}

void MyPushButtonCopy::setDisplayDefault(bool displayDefau)
{
    if(d->displayDefault == displayDefau)
        return;
    d->displayDefault = displayDefau;
    this->update();
}

/*!
    \property MyPushButtonCopy::getPushButtonOperate
    \brief the Operation of the PushButton

    The default getPushButtonOperate is Set_ON.

    \sa indent
*/
My::PushButtonCopyOperate MyPushButtonCopy::getPushButtonCopyOperate() const
{
    return d->pushButtonCopyOperate;
}

void MyPushButtonCopy::setPushButtonCopyOperate(My::PushButtonCopyOperate operate)
{
    if(d->pushButtonCopyOperate == operate)
        return;

    d->pushButtonCopyOperate = operate;
    this->update();
}

/*!
    \property MyPushButtonCopy::getCoilOfRegisterIndex
    \brief the Index of the coil in register

    The default coilOfRegisterIndex is 0.

    \sa indent
*/
int MyPushButtonCopy::getCoilOfRegisterIndex() const
{
    return d->coilOfRegisterIndex;
}

void MyPushButtonCopy::setCoilOfRegisterIndex(int index)
{
    if(d->coilOfRegisterIndex == index)
        return;

    d->coilOfRegisterIndex = index;
}

/*!
    \reimp
*/
void MyPushButtonCopy::paintEvent(QPaintEvent *ev)
{
    //绘制准备工作,启用反锯齿
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

    if(d->displayDefault)
    {
        switch (d->pushButtonCopyOperate)
        {
        case My::Coil_Copy:
            this->setText("CopyCoil");
            break;
        case My::Registers_Copy:
            this->setText("CopyRegister");
            break;
        default:
            return;
        }
    }

    QPushButton::paintEvent(ev);
}

/*!
 * \brief MyLabel::enterEvent
 * \param ev
 */
void MyPushButtonCopy::enterEvent(QEvent *ev)
{
    if(QEvent::Enter == ev->type())
    {
        QCursor cursor;                           //创建光标对象
        cursor.setShape(Qt::PointingHandCursor);  //设置光标形状(指)
        setCursor(cursor);                        //使用光标

        update();
    }
}

/*!
 * \brief MyLabel::leaveEvent
 * \param ev
 */
void MyPushButtonCopy::leaveEvent(QEvent *ev)
{
    if(QEvent::Leave == ev->type())
    {
        QCursor cursor;                           //创建光标对象
        cursor.setShape(Qt::ArrowCursor);         //设置光标形状(指)
        setCursor(cursor);                        //使用光标

        update();
    }
}

/*!
 * \brief MyPushButtonCopy::mousePressEvent
 * \param ev
 */
void MyPushButtonCopy::mousePressEvent(QMouseEvent *ev)
{
    if(Qt::LeftButton == ev->button())
    {
        d->LeftmousePressed = true;
        d->mousePressPoint = ev->globalPos();

        QCursor cursor;                         //创建光标对象
        cursor.setShape(Qt::ClosedHandCursor);  //设置光标形状(小手)
        setCursor(cursor);                      //使用光标

        update();
    }
}

/*!
 * \brief MyPushButtonCopy::mouseReleaseEvent
 * \param ev
 */
void MyPushButtonCopy::mouseReleaseEvent(QMouseEvent *ev)
{
    if(Qt::LeftButton == ev->button())
    {
        d->LeftmousePressed = false;

        QCursor cursor;                           //创建光标对象
        cursor.setShape(Qt::PointingHandCursor);  //设置光标形状(指)
        setCursor(cursor);                        //使用光标

        update();
    }

}

/*!
 * \brief MyPushButtonCopy::mouseMoveEvent
 * \param ev
 */
void MyPushButtonCopy::mouseMoveEvent(QMouseEvent *ev)
{
    // 鼠标左键拖动移动
    if(d->LeftmousePressed)
    {
        double mouseDx = ev->globalPos().x() - d->mousePressPoint.x();
        double mouseDy = ev->globalPos().y() - d->mousePressPoint.y();
        d->mousePressPoint = ev->globalPos();

        double myLabelNewx = this->x() + mouseDx;
        double myLabelNewy = this->y() + mouseDy;

        if(myLabelNewx < 0)
            myLabelNewx = 0;
        if(myLabelNewx + this->width() > QApplication::activeWindow()->width())
            myLabelNewx = QApplication::activeWindow()->width() - this->width();
        if(myLabelNewy < 0)
            myLabelNewy = 0;
        if(myLabelNewy + this->height() > QApplication::activeWindow()->height())
            myLabelNewy = QApplication::activeWindow()->height() - this->height();

        move(myLabelNewx, myLabelNewy);
        update();
    }
}
